package weaver.aiyh_pcn.fadada;

import aiyh.utils.Util;
import aiyh.utils.zwl.common.ToolUtil;
import com.api.aiyh_pcn.fadada.dao.FaDDContractMapping;
import com.api.aiyh_pcn.fadada.entity.FaDaDaConfigDTO;
import org.h2.util.StringUtils;
import weaver.aiyh_pcn.fadada.util.FaddContractUtil;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.List;
import java.util.Map;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/11/3 0003 15:20
 */


public class FaDDContractAction extends ToolUtil implements Action {
	private final FaDDContractMapping faDDContractMapping = new FaDDContractMapping();
	private final ToolUtil toolUtil = new ToolUtil();
	//	合同字段
	private String contractField;
	//	客户编号字段
	private String customerNoField;
	//	客户编号
	private String customerNo;
	//	是否是顺序签订
	private String queueSigned;
	//	签订字段,用于是否电子签
	private String signedField;
	//	签订值
	private String signedValue;


	@Override
	public String execute(RequestInfo requestInfo) {
//		获取user
		User user = requestInfo.getRequestManager().getUser();
//		获取流程id
		String workflowId = requestInfo.getWorkflowid();
//		获取请求id
		String requestId = requestInfo.getRequestid();
//		获取主表名称
		String mainTableName = requestInfo.getRequestManager().getBillTableName();
//		获取法大大配置信息
		FaDaDaConfigDTO faDaDaConfigDTO = faDDContractMapping.queryConfig(workflowId, 2);
//		获取明细数据在那个表
		String tableName = faDaDaConfigDTO.getCheckSource();
//		查询主表信息
		Map<String, Object> mainMap = faDDContractMapping.queryMainMap(requestId, mainTableName);
//		判断主表中的电子签章类型
		String value = Util.null2String(mainMap.get(signedField));
		if (StringUtils.isNullOrEmpty(value)) {
			value = String.valueOf(System.currentTimeMillis());
		}
//		如果不需要进行电子签章，则放行流程
		if (!value.equals(signedValue)) {
			return Action.SUCCESS;
		}
		String mainId = Util.null2String(mainMap.get("id"));
//      查询明细数据
		List<Map<String, Object>> detailMaps = faDDContractMapping.queryDetailMaps(mainId, tableName);
//		开始签署合同
		FaddContractUtil faddContractUtil = new FaddContractUtil(workflowId, requestId, mainTableName, tableName,
				customerNo, customerNoField, contractField, queueSigned, detailMaps, mainMap, user);
		try {
			faddContractUtil.createContract(mainMap);
		} catch (Exception e) {
			toolUtil.writeErrorLog("合同创建失败，失败原因：" + e);
//			TODO 更换labelIndex
			requestInfo.getRequestManager().setMessagecontent(Util.getHtmlLabelName(-87660, user.getLanguage(), "合同创建失败，未能正确创建法大大合同！"));
			requestInfo.getRequestManager().setMessageid("1");
			return Action.FAILURE_AND_CONTINUE;
		}
		if (faDaDaConfigDTO.getSignedType() == 1) {
//			需要控制签署规则
			if (Integer.parseInt(Util.null2String(mainMap.get(faDaDaConfigDTO.getFieldControl1()))) == faDaDaConfigDTO.getOnlyOwn()) {
//				只需要本方签署
				return Action.SUCCESS;
			}

/*
			if(Integer.parseInt(Util.null2String(mainMap.get(faDaDaConfigDTO.getFieldControl1()))) == faDaDaConfigDTO.getOnlyOther()){
//				只需要对方签署
			}
//			双方签署
*/

		}
		try {
			faddContractUtil.signedContract(mainMap);
		} catch (Exception e) {
			toolUtil.writeErrorLog("合同已创建，但发起签署失败，失败原因：" + e);
//			TODO 更换labelIndex
			requestInfo.getRequestManager().setMessagecontent(Util.getHtmlLabelName(-87659, user.getLanguage(), "合同已创建，当合同发起签署时失败！"));
			requestInfo.getRequestManager().setMessageid("2");
			return Action.FAILURE_AND_CONTINUE;
		}
		return Action.SUCCESS;
	}


	public String getContractField() {
		return contractField;
	}

	public void setContractField(String contractField) {
		this.contractField = contractField;
	}

	public String getCustomerNoField() {
		return customerNoField;
	}

	public void setCustomerNoField(String customerNoField) {
		this.customerNoField = customerNoField;
	}

	public String getCustomerNo() {
		return customerNo;
	}

	public void setCustomerNo(String customerNo) {
		this.customerNo = customerNo;
	}

	public String getSignedField() {
		return signedField;
	}

	public void setSignedField(String signedField) {
		this.signedField = signedField;
	}

	public String getSignedValue() {
		return signedValue;
	}

	public void setSignedValue(String signedValue) {
		this.signedValue = signedValue;
	}

	public String getQueueSigned() {
		return queueSigned;
	}

	public void setQueueSigned(String queueSigned) {
		this.queueSigned = queueSigned;
	}
}
